package com.vgooo.client.tool.branch;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.vgooo.bean.Branch;
import com.vgooo.client.R;
import com.vgooo.client.tool.branch.BrandsMainActivity.Data;
import com.vgooo.util.DebugTools;

public class BranchAdapter extends BaseExpandableListAdapter {
	private static String className = "BranchAdapter" ;
	private Data data ;
	private List<String> parents;//店铺父列表数据
	private List<List<Branch>> childs ;//店铺子列表数据
	private LayoutInflater inflater ;
	private Context context ;
	
	
	
	public BranchAdapter(Data data,Context context) {
		super();
//		this.parents = parents;
//		this.childs = childs;
		setParents(data.parents);
		setChilds(data.childs);
		this.context = context ;
		inflater = LayoutInflater.from(context);
	}
	
	
	
	
	
	public void setParents(List<String> parents) {
		if(parents!=null&&!parents.isEmpty()){
			this.parents = parents;
		}else{
			this.parents = new ArrayList<String>();
		}
	}





	public void setChilds(List<List<Branch>> childs) {
		if(childs!=null&&!childs.isEmpty()){
			this.childs = childs;
		}else{
			this.childs = new ArrayList<List<Branch>>() ;
		}
	}





	public void dataChanage(Data data){
		if(data.parents!=null&&data.childs!=null&&!data.parents.isEmpty()&&!data.childs.isEmpty()){
//			this.parents.clear() ;
//			this.childs.clear();
			this.parents = data.parents ;
			this.childs = data.childs ;
			notifyDataSetChanged();
		}
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return childs.get(groupPosition).get(childPosition);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return Long.parseLong(childs.get(groupPosition).get(childPosition).branch_id);
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		Branch branch = childs.get(groupPosition).get(childPosition) ;
		ChildViewHolder holder = null;
		if(convertView == null){
			convertView = inflater.inflate(R.layout.branch_item_child, null);
			holder = new ChildViewHolder() ;
			holder.tvName = (TextView) convertView.findViewById(R.id.branch_item_child_tv);
			holder.tvMap = (TextView) convertView.findViewById(R.id.branch_item_child_view_map);
			holder.ivDetail = (ImageView) convertView.findViewById(R.id.branch_item_child_view_detail);
			
			holder.layout = (RelativeLayout) convertView.findViewById(R.id.branch_item_bottom);
			
			holder.tvAddr = (TextView) convertView.findViewById(R.id.branch_item_addr_body);
			holder.tvPhone = (TextView) convertView.findViewById(R.id.branch_item_phone_body);
			holder.tvTime = (TextView) convertView.findViewById(R.id.branch_item_time_body);
			holder.tvMetro = (TextView) convertView.findViewById(R.id.branch_item_metro_body);
			holder.tvBus = (TextView) convertView.findViewById(R.id.branch_item_bus_body);
			convertView.setTag(holder);
		}else{
			holder = (ChildViewHolder) convertView.getTag() ;
		}
		holder.tvName.setText(branch.branch_name);
		holder.tvAddr.setText(branch.branch_addr);
		holder.tvPhone.setText(branch.branch_tel);
		holder.tvTime.setText(branch.branch_time);
		holder.tvMetro.setText(branch.metro);
		holder.tvBus.setText(branch.bus);
		holder.tvMap.setTag(branch.branch_jw);
//		holder.tvMap.setVisibility(View.GONE);
//		holder.ivDetail.setVisibility(View.VISIBLE);
//		holder.layout.setVisibility(View.GONE);
		if(branch.isDetail){
			holder.layout.setVisibility(View.VISIBLE);
			holder.ivDetail.setVisibility(View.GONE);
			holder.tvMap.setVisibility(View.VISIBLE);
		}else{
			holder.layout.setVisibility(View.GONE);
			holder.ivDetail.setVisibility(View.VISIBLE);
			holder.tvMap.setVisibility(View.GONE);
		}
		return convertView;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		// TODO Auto-generated method stub
		return childs.get(groupPosition).size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		// TODO Auto-generated method stub
		return childs.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		// TODO Auto-generated method stub
		return parents.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		// TODO Auto-generated method stub
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ParentViewHolder holder = null;
		if(convertView == null){
			convertView = inflater.inflate(R.layout.branch_item_parent, null);
			holder = new ParentViewHolder() ;
			holder.tv = (TextView) convertView.findViewById(R.id.branch_item_parent_tv);
			holder.iv = (ImageView) convertView.findViewById(R.id.branch_item_parent_ex);
			convertView.setTag(holder);
		}else{
			holder = (ParentViewHolder) convertView.getTag() ;
		}
		holder.tv.setText(parents.get(groupPosition));
		
		if(isExpanded){
			holder.iv.setBackgroundResource(R.drawable.ex_lv_ex);
		}else{
			holder.iv.setBackgroundResource(R.drawable.ex_lv_ex_no);
		}
		
		return convertView;
	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
//		DebugTools.log(className, "isChildSelectable groupPosition = " + groupPosition + " childPosition = " + childPosition);
		return true;
	}
	
	class ChildViewHolder {
		TextView tvName ;
		ImageView ivDetail;
		TextView tvAddr;
		TextView tvTime ;
		TextView tvPhone;
		TextView tvMetro ;
		TextView tvBus ;
		TextView tvMap ;
		RelativeLayout layout ;
	}
	
	class ParentViewHolder{
		TextView tv ;
		ImageView iv ;
	}

}
